package com.xca.is.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.xca.is.R;
import com.xca.is.adapter.TrainAdapter;
import com.xca.is.databinding.ActivityTrainBinding;
import com.xca.is.entity.TrainEntity;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.TrainModel;
import com.xca.is.utils.EditTextUtils;
import com.xca.is.utils.WebViewScriptUtils;

import java.util.ArrayList;
import java.util.List;

public class TrainActivity extends AppCompatActivity implements HttpConnect {
    private ActivityTrainBinding binding ;
    private TrainAdapter adapter ;
    private ViewModelProvider provider ;
    private TrainModel model ;
    private MutableLiveData<TrainEntity> liveData ;
    private List<TrainEntity.DataDTO> list ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTrainBinding.inflate(getLayoutInflater()) ;
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        binding.trainLoading.rLoading.setVisibility(View.GONE);
        binding.trainError.queryError.setVisibility(View.GONE);

        int[] ints = new int[]{10,18} ;
        WebViewScriptUtils.htmlContent(ints,binding.trainWeb);

        list = new ArrayList<>() ;
        binding.trainRecycler.setLayoutManager(new GridLayoutManager(this , 1));
        binding.trainRecycler.setHasFixedSize(true); // 优化 RecyclerView
        provider = new ViewModelProvider(this) ;
        model = provider.get(TrainModel.class) ;
        model.setModel(this);
        liveData = (MutableLiveData<TrainEntity>) model.getLiveData() ;
        liveData.observe(this, new Observer<TrainEntity>() {
            @Override
            public void onChanged(TrainEntity trainEntity) {
                if (trainEntity.getCode()==200){
                    list = trainEntity.getData() ;
                    adapter.updateList(list);
                }else {
                    binding.trainError.queryError.setVisibility(View.VISIBLE);
                }
            }
        });

        adapter = new TrainAdapter(list, this);
        binding.trainRecycler.setAdapter(adapter);

        onClickListener();
    }

    private void onClickListener(){
        binding.trainSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String go = binding.trainStart.getText().toString() ;
                String to = binding.trainEnd.getText().toString() ;
                String time = binding.trainTime.getText().toString() ;
                if (go.isEmpty()){
                    binding.trainStart.setError("请输入出发地");
                    return;
                }
                if (to.isEmpty()){
                    binding.trainEnd.setError("请输入目的地");
                    return;
                }
                if (time.isEmpty()){
                    binding.trainTime.setError("请输入出发时间");
                    return;
                }
                model.getData(go, to, time);
                EditTextUtils.clearFocus(binding.trainStart);
                EditTextUtils.clearFocus(binding.trainEnd);
                EditTextUtils.clearFocus(binding.trainTime);
                binding.trainError.queryError.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void Loading(boolean loading) {
        binding.trainLoading.rLoading.setVisibility(loading? View.VISIBLE:View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {
        if (!msg.isEmpty()){
            binding.trainError.queryError.setVisibility(View.VISIBLE);
            binding.trainLoading.rLoading.setVisibility(View.GONE);
        }

    }

    @Override
    public void Complete() {
        binding.trainLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}